package com.mingle.iwalk;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GalleryAdapter extends BaseAdapter {

	private List<POIInfo> mDataList;
	private Context mContext;
	private Activity mActivity;
	private LayoutInflater mInflater;
	private boolean mbShowPanel;
	
	public GalleryAdapter(Context context, Activity activity, List<POIInfo> dataList)
	{
		mDataList = dataList;
		mContext = context;
		mActivity = activity;
		mInflater = LayoutInflater.from(context);
		
		mbShowPanel = true;
	}
	
	@Override
	public int getCount() {
		
		return mDataList.size();
	}

	@Override
	public Object getItem(int pos) {
		
		return mDataList.get(pos);
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		POIInfo info = mDataList.get(position);
		ImageView im = null;
		TextView desciption = null;
		TextView theme = null;
		TextView time = null;
		
		if(convertView == null)
		{	
			convertView = mInflater.inflate(R.layout.picture_gallery_item_layout, null);
			convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			Button exitBtn = (Button)convertView.findViewById(R.id.exit_btn);
			exitBtn.setOnClickListener(mExitListener);
		}

		im = (ImageView) convertView.findViewById(R.id.picture);
		desciption = (TextView)convertView.findViewById(R.id.description);
		theme = (TextView) convertView.findViewById(R.id.theme);
		time = (TextView) convertView.findViewById(R.id.time);
		
		im.setImageBitmap(info.picThumb);
		desciption.setText(info.description);
		theme.setText(info.title);
		long intTime = info.createTime;
		SimpleDateFormat sdfTimea = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");   
		String strTime = sdfTimea.format(new Date(intTime));
		time.setText(strTime);
		
		View controlPanel = (View) convertView.findViewById(R.id.pic_control_panel);
		controlPanel.setVisibility(mbShowPanel?View.VISIBLE:View.INVISIBLE);
		
		return convertView;
	}
	
	private OnClickListener mExitListener = new OnClickListener()
    {

		@Override
		public void onClick(View v) {
			
			mActivity.finish();
		}
    	
    };
    
    public void toggleControlPanel()
    {
    	mbShowPanel = !mbShowPanel;
    	notifyDataSetChanged();
    }

}
